import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sample2/helper/app_helper.dart';

class Settings extends StatefulWidget {
  const Settings({super.key});
  @override
  State<StatefulWidget> createState() {
    return SettingsState();
  }
}

class SettingsState extends State {
  String batteryLevel = "";
  int languageIndex = 0;
  static const platform = MethodChannel("sample.flutter.dev/battery");

  @override
  void initState() {
    _getBatteryLevel();
    _getLanguageIndex();
    super.initState();
  }

  _getLanguageIndex() {
    AppHelper.shared.getCurrentLanguageCode().then((value) {
      debugPrint("got language code: $value");
      int lanIdx = -1;
      if (value != null && value.isNotEmpty) {
        lanIdx = ['en', 'zh'].indexOf(value);
      }
      setState(() {
        languageIndex = lanIdx;
      });
    });
  }

  Future<void> _getBatteryLevel() async {
    try {
      final int result = await platform.invokeMethod("getBatteryLevel");
      batteryLevel = "$result %";
    } on PlatformException catch (e) {
      batteryLevel = "can not detect battery level now. ${e.message}";
    }
    setState(() {
      batteryLevel = batteryLevel;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Settings'),
      ),
      body: ListView(
        children: <Widget>[
          Container(
            height: 44,
            width: MediaQuery.of(context).size.width,
            decoration: const BoxDecoration(
              color: Color.fromARGB(255, 255, 177, 59),
            ),
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
            child: Align(
              alignment: Alignment.centerLeft,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const SizedBox(
                    width: 100,
                    child: Text("Battery Level: "),
                  ),
                  const Text.rich(TextSpan(
                      style: TextStyle(color: Colors.red), text: "-> ")),
                  Expanded(
                    child: Text(
                      batteryLevel,
                      maxLines: 3,
                      textAlign: TextAlign.right,
                      style: const TextStyle(backgroundColor: Colors.blue),
                    ),
                  )
                ],
              ),
            ),
          ),
          Container(
            height: 44,
            width: MediaQuery.of(context).size.width,
            decoration: const BoxDecoration(
              color: Colors.yellow,
            ),
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const SizedBox(
                  width: 100,
                  child: Text(
                    "Notification:",
                    style: TextStyle(color: Colors.black),
                  ),
                ),
                Expanded(
                    child: Container(
                  alignment: Alignment.centerRight,
                  decoration: const BoxDecoration(color: Colors.green),
                  child: Switch(
                    thumbColor: MaterialStateProperty.resolveWith(
                        (states) => Colors.red),
                    value: false,
                    onChanged: (value) {},
                  ),
                ))
              ],
            ),
          ),
          Container(
            height: 44,
            width: MediaQuery.of(context).size.width,
            decoration: const BoxDecoration(
              color: Colors.yellow,
            ),
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const SizedBox(
                  width: 100,
                  child: Text(
                    "Language:",
                    style: TextStyle(color: Colors.black),
                  ),
                ),
                Expanded(
                    child: Container(
                        alignment: Alignment.centerRight,
                        decoration: const BoxDecoration(color: Colors.green),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            const Text("EN"),
                            Checkbox(
                              value: languageIndex == 0,
                              onChanged: (value) {
                                setState(() {
                                  languageIndex = 0;
                                });
                                AppHelper.shared.updateCurrentLocale(
                                    context, const Locale("en"));
                              },
                            ),
                            const Text("ZH"),
                            Checkbox(
                              value: languageIndex == 1,
                              onChanged: (value) {
                                setState(() {
                                  languageIndex = 1;
                                });
                                AppHelper.shared.updateCurrentLocale(
                                    context, const Locale("zh"));
                              },
                            ),
                            const Text("System"),
                            Checkbox(
                              value: languageIndex == -1,
                              onChanged: (value) {
                                setState(() {
                                  languageIndex = -1;
                                });
                                AppHelper.shared
                                    .updateCurrentLocale(context, null);
                              },
                            )
                          ],
                        )))
              ],
            ),
          )
        ],
      ),
    );
  }
}
